package com.rui.emp.controller;

import com.rui.common.vo.Result;
import com.rui.emp.entity.Emp;
import com.rui.emp.service.EmpService;
import com.rui.emp.vo.EmpQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping("")
    public  String toEmpListUI(){
        return "emp/empList";
    }

    @GetMapping("/list")
    @ResponseBody
    public Result<Object> getEmpList(EmpQuery param){
        List<Emp> list = empService.getEmpList(param);//查出员工信息
        Long count = empService.countEmpList(param);   //分页
        return Result.success(list, count);
    }
    @PostMapping("")//新增数据时，用post请求
    @ResponseBody
    public Result<Object> addemp(Emp emp){
        empService.addEmp(emp);
        return Result.success("添加员工信息成功！");
}
    @GetMapping("add/ui")
    public String toAddUI(Model model){
        return "emp/addEmp";
    }

    @DeleteMapping("/{ids}")
    @ResponseBody
    public Result<Object> delectEmpByIds(@PathVariable("ids") String ids){
        empService.delectEmpByIds(ids);
        return Result.success("删除员工成功");
    }

    @GetMapping("/{id}")
    public String getEmpByid(@PathVariable("id") Integer id,Model model){
        Emp emp =  empService.getEmpById(id);
        model.addAttribute("emp",emp);
        return "emp/empEdit";
    }
    @PutMapping("")
    @ResponseBody
    public Result<Object> updateEmp(Emp emp ){
        empService.updateEmp(emp);
        return  Result.success("员工信息修改成功");
    }

}
